//
//  YZGAlertView.m
//  Masonry
//
//  Created by LXY on 16/6/14.
//  Copyright © 2016年 AVGD. All rights reserved.
//

#import "MJUIAlertVCBaseView.h"
@interface MJUIAlertVCBaseView()<UIGestureRecognizerDelegate>


@end

@implementation MJUIAlertVCBaseView



#pragma mark **************** init
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
     
        [self initUI];

    }
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    [self initUI];
}
- (void)initUI{
    self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissWindow)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];//添加手势
}
- (void)initContentview:(UIView*)view {
    self.contentview = view;
    if(self.alertType == AlertTypeCenter){
        self.contentview.mj_cornerRadius = 15;
    }else{
        self.contentview.transform = CGAffineTransformMakeTranslation(0, self.contentHeight);
    }
    self.contentview.alpha = 0;
    [self show];
}
#pragma mark ************** 显示
- (void)show {
    MJUIAfter_GCD(0.05, ^{

        if(self.alertType == AlertTypeCenter){
            [UIView animateWithDuration:0.3f animations:^{
                [self setBackgroundColor:self.mBackgroundColor ? self.mBackgroundColor:UIColor.mj_windowColor];
                self.contentview.alpha = 1;
                
            } completion:nil];
        }else{
            [UIView animateWithDuration:0.3f animations:^{

                [self setBackgroundColor:self.mBackgroundColor ? self.mBackgroundColor:UIColor.mj_windowColor];
                self.contentview.alpha = 1;
                self.contentview.transform = CGAffineTransformMakeTranslation(0, 0);

            } completion:nil];
        }
    });
  
    
}
#pragma mark ************** 消失
- (void)dismissWindow
{
    if(self.enableDismissWindow){
        [self dismiss];
    }
}
- (void)dismiss
{
    
    if(self.alertType == AlertTypeCenter){
        [UIView animateWithDuration:0.3f animations:^{
            self.backgroundColor = MJUIColorMakeWithRGBA(0, 0, 0, 0);
            self.contentview.alpha = 0;

        } completion:^(BOOL finished) {
            if(self.handleDismissBlock){
                self.handleDismissBlock();
            }
            [self.mj_viewController dismissViewControllerAnimated:false completion:nil];
        }];
    }else{
        
        [UIView animateWithDuration:0.3f animations:^{
            self.backgroundColor = MJUIColorMakeWithRGBA(0, 0, 0, 0);
            self.contentview.transform = CGAffineTransformMakeTranslation(0, self.contentHeight);

        } completion:^(BOOL finished) {
            if(self.handleDismissBlock){
                self.handleDismissBlock();
            }
            [self.mj_viewController dismissViewControllerAnimated:false completion:nil];

        }];
    }
    
}

- (void)dismissCompletion: (void (^)(void))completion{
    
    if(self.alertType == AlertTypeCenter){
        [UIView animateWithDuration:0.3f animations:^{
            self.backgroundColor = MJUIColorMakeWithRGBA(0, 0, 0, 0);
            self.contentview.alpha = 0;

        } completion:^(BOOL finished) {
            if(self.handleDismissBlock){
                self.handleDismissBlock();
            }
            [self.mj_viewController dismissViewControllerAnimated:false completion:^{
                completion();
            }];
        }];
    }else{
        
        [UIView animateWithDuration:0.3f animations:^{
            self.backgroundColor = MJUIColorMakeWithRGBA(0, 0, 0, 0);
            self.contentview.transform = CGAffineTransformMakeTranslation(0, self.contentHeight);

        } completion:^(BOOL finished) {
            if(self.handleDismissBlock){
                self.handleDismissBlock();
            }
            [self.mj_viewController dismissViewControllerAnimated:false completion:^{
                completion();
            }];

        }];
    }
    
}

#pragma mark ************** 父控件布局完成
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if(self.alertType == AlertTypeBottom){
        [self.contentview.superview layoutIfNeeded];
        [self.contentview mj_cornerWithType:MJCornerRadiusTypeTop cornerRadius:16];
    }
}
#pragma mark ************** 子视图不响应父视图手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([touch.view isDescendantOfView:self.contentview]) {
        return NO;
    }
    
    return YES;
    
}

@end
